package com.turing.post.thirdinterface.impl;

import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.thirdinterface.util.MapCardTradDetail;
import com.turing.post.thirdinterface.util.MapCardTradDetailDao;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;

/**
 * @author zxp
 * @Jan 27, 2013
 */
@Stateless(mappedName = "thirdinterface.impl.MapCardTradDetailDaoImpl")
@Remote
@Local
public class MapCardTradDetailDaoImpl implements MapCardTradDetailDao {
	/**
	 * 实体管理器
	 * 
	 * @see javax.persistence.EntityManager
	 */
	@PersistenceContext
	protected EntityManager _em;

	@Override
	public void insertMapCardTradDetail(MapCardTradDetail mctd)
			throws PCPSApplicationException {
		GeneralDao.insert(_em, mctd);
	}

	@SuppressWarnings("unchecked")
	@Override
	public String getMaxTranseqno() {
		String sql = "select max(Transeqno) from MapCardTradDetail";
		Query query = _em.createNativeQuery(sql);
		if (query.getSingleResult() == null) {
			return null;
		} else {
			return query.getSingleResult().toString();
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public String getTranseqno(String transeqno) {
		String sql = "select Transeqno from MapCardTradDetail where Transeqno='"
				+ transeqno + "'";
		Query query = _em.createNativeQuery(sql,MapCardTradDetail.class);
		List<MapCardTradDetail> mctdList = query.getResultList();
		if (mctdList.size() == 1) {
			return mctdList.get(0).getTranseqno();
		} else {
			return null;
		}
	}
}
